package net.skycloud.f5.util;

import org.springframework.beans.support.PagedListHolder;
import org.springframework.data.domain.Page;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author xiongmin
 * @Description //TODO
 * @Date 2019/12/18 17:14
 * @Version 1.0
 **/
public class PageInfo<T> {
    private List<T> list;
    private long count;
    private int pageSize;
    private int allPage;

    public PageInfo(Page<T> page) {
        if (page != null) {
            this.list = page.getContent();
            this.count = page.getTotalElements();
            this.pageSize = page.getSize();
            this.allPage = page.getTotalPages();
        }
    }

    public PageInfo(PagedListHolder<T> page, long count, int currentPage) {
        if (page != null) {
            if (currentPage >= page.getPageCount()) {
                this.list = new ArrayList<>();
                this.count = count;
                this.pageSize = page.getPageSize();
                this.allPage = page.getPageCount();
            } else {
                this.list = page.getPageList();
                this.count = count;
                this.pageSize = page.getPageSize();
                this.allPage = page.getPageCount();
            }

        }
    }

    public PageInfo() {

    }

    /*
     * list 是所有的结果
     * */
    public PageInfo<T> pageable(List<T> list, int page, int pageSize) {
        page = page < 0 ? 0 : page;
        pageSize = pageSize < 1 ? 1 : pageSize;
        int from = page * pageSize;
        int to = from + pageSize;
        List<T> subList = null;
        if (list == null || list.isEmpty()) {
            list = Collections.emptyList();
        }
        int size = list.size();
        if (size >= from) {
            if (size >= to) {
                subList = list.subList(from, to);
            } else {
                subList = list.subList(from, size);
            }
        } else {
            subList = Collections.emptyList();
        }
        this.setCount(list.size());
        this.setList(subList);
        this.setPageSize(pageSize);
        this.setAllPage((int) Math.ceil(list.size() / (pageSize * 1.0)));
        return this;
    }

    /*
     * list 是分页好的结果
     * */
    public PageInfo(List<T> list, long count, int pageSize) {
        this.list = list;
        this.count = count;
        this.pageSize = pageSize;
        this.setAllPage((int) Math.ceil(count / (pageSize * 1.0)));
    }

    public List<T> getList() {

        if (list == null) {
            return Collections.emptyList();
        }

        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getAllPage() {
        return allPage;
    }

    public void setAllPage(int allPage) {
        this.allPage = allPage;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "list=" + list +
                ", count=" + count +
                ", pageSize=" + pageSize +
                ", allPage=" + allPage +
                '}';
    }
}

